package com.qlslylq.ad.example.manager;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import static com.qlslylq.ad.example.constant.RequestCode.REQUEST_CODE_NEED;

/**
 * 动态权限申请管理<br/>
 */
public class PermissionManager {

    /**
     * 检验进入所需权限 <br/>
     */
    public static boolean checkNeedPermission(Activity activity) {
        String[] permissions = new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
        };

        if (Build.VERSION.SDK_INT > 22) {
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE_NEED);
                    return false;
                }
            }
            return true;
        } else {
            return true;
        }
    }

    /**
     * 相册权限
     */
    public static boolean checkPicturePermission(Activity activity) {
        String[] permissions = new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (Build.VERSION.SDK_INT > 22) {
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE_NEED);
                    return false;
                }
            }
            return true;
        } else {
            return true;
        }

    }

    /**
     * 定位权限
     */
    public static boolean checkGPSPermission(Activity activity) {
        String[] permissions = new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        if (Build.VERSION.SDK_INT > 22) {
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE_NEED);
                    return false;
                }
            }
            return true;
        } else {
            return true;
        }

    }

    /**
     * 检验安装权限<br/>
     */
    public static boolean checkInstallPermission(Context context) {
        boolean hasPermission = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            hasPermission = context.getPackageManager().canRequestPackageInstalls();
        }
        return hasPermission;
    }

    /**
     * 检验修改系统设置权限<br/>
     */
    public static boolean checkWriteSettingPermission(Context context) {
        boolean hasPermission = true;
        if (Build.VERSION.SDK_INT >= 29) {//10.0才检查
            if (Settings.System.canWrite(context)) {
                hasPermission = true;
            } else {
                hasPermission = false;
            }
        }
        return hasPermission;
    }

    public interface OnPermissionListennter {
        void onResult(boolean isTrue);
    }

}
